package xyz.zeer.endangeredanimal;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class InfoActivity extends AppCompatActivity {

    TextView nameTextView, infoTextView, statusTextView;
    ImageView imageView, back ,speech;
    private TextToSpeech textToSpeech;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yrs_activity_info);

        nameTextView = findViewById(R.id.textView_nameHome);
        infoTextView = findViewById(R.id.textViewInfo);
        statusTextView = findViewById(R.id.textViewStatus);
        imageView = findViewById(R.id.imageViewHome);
        back = findViewById(R.id.ButtonBack);
        speech = findViewById(R.id.ButtonSpeech);

        final Intent i = getIntent();
        if(i.getExtras() != null)
        {
            int urlImage = i.getIntExtra("ImageURL", R.drawable.info);
            String animalN = i.getStringExtra("AnimalName");
            String animal_info = i.getStringExtra("AnimalInfo");
            String status = i.getStringExtra("AnimalStatus");
            String other = i.getStringExtra("AnimalOtherInfo");
            String effort = i.getStringExtra("WWFEfforts");

            nameTextView.setText(animalN);
            imageView.setImageResource(urlImage);
            infoTextView.setText(animal_info);
            statusTextView.setText(status);
        }

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InfoActivity.this.finish();
            }
        });

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(InfoActivity.this, ImageActivity.class);
                intent.putExtra("Image", i.getIntExtra("ImageURL", R.drawable.info));
                Log.e("InfoActivity", "image selected: " + i.getStringExtra("AnimalName"));
                startActivity(intent);
            }
        });

        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status == TextToSpeech.SUCCESS)
                {
                    int result = textToSpeech.setLanguage(Locale.US);

                    if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
                    {
                        Log.e("TTS", "onInit: " + "Language not supported");
                    }
                    else
                    {
                        speech.setEnabled(true);
                    }
                }
                else
                {
                    Log.e("TTS", "onInit: " + "Initialization failed");
                }
            }
        });

        speech.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speechText();
            }
        });
    }

    private void speechText() {
        String text = infoTextView.getText().toString();
        float speed = 1f;
        float pitch = 1f;
        textToSpeech.setSpeechRate(speed);
        textToSpeech.setPitch(pitch);
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    @SuppressLint("SuspiciousIndentation")
    @Override
    protected void onDestroy() {
        if(textToSpeech != null)
            textToSpeech.stop();
        textToSpeech.shutdown();

        super.onDestroy();
    }
}